/**
 * 数字输入框
 *
 * @Author: Shann
 * @LastEditors: Shann
 * @Date: 2021-07-28 10:06:44
 * @LastEditTime: 2021-07-29 11:23:13
 * @FilePath: \angular-js-for-bootstrap\src\Essa\InputNumber\InputNumber.js
 * @Copyright: Copyright 2021-2021, all rights reserved. Essa.cn
 */
class InputNumber {
    props = {
        defaultValue: Number,
        min: Number,
        max: Number,
        onChange: Function,
        value: Number,
        precision: Number,
        key: Number,
    };

    state = {
        value: Number(this.props.defaultValue) || null,
        min: Number(this.props.min),
        max: Number(this.props.max),
        precision: Number(this.props.precision),
        key: Number(this.props.key),
    };

    handleChange() {
        let func = this.props.onChange();
        if (typeof func === "function") {
            func(this.state.value, this.state.key);
        }
    }

    toFixed(value, number = 2) {
        return Math.floor(value * Math.pow(10, number)) / Math.pow(10, number);
    }

    setValue(value) {
        this.state.value = value;
        scope.handleChange();
    }

    handleBlur(event) {
        let value = Number(event.target.value);
        if (this.state.max && value > this.state.max) {
            scope.setValue(this.state.max);
            return;
        }
        if (this.state.min && value < this.state.min) {
            scope.setValue(this.state.min);
            return;
        }
        if (this.state.precision === 0) {
            scope.setValue(parseInt(value));
        }
        if (this.state.precision > 0) {
            scope.setValue(
                scope.toFixed(value, this.state.precision)
            );
        }
    }
}
